import TR_ch from "../../../util/languages/ch";
import TR_en from "../../../util/languages/en";

class I18n {
    constructor() {
        this.locale = LanguageEnum.CHINESE;
        this.messages = {
            chinese: {message: TR_ch},
            english: {message: TR_en}
        }
    }

    setLocale(locale = LanguageEnum.CHINESE) {
        this.locale = locale;
        if (locale === LanguageEnum.ENGLISH) {
            document.documentElement.classList.add('english');
        } else {
            document.documentElement.classList.remove('english');
        }
    }

    getMessage(msgPath) {
        if (!msgPath) {
            return '';
        }
        let languageJson = this.messages[this.locale];
        return this.get(languageJson, msgPath.split('.')) || msgPath
    }

     get(object, path) {
        return path.reduce((xs, x) => (xs && xs[x]) ? xs[x] : null, object);
    }
}
export const LanguageEnum = {
    CHINESE: 'chinese',
    ENGLISH: 'english',
}

export const i18n = new I18n();